﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using Settings;

namespace djs_mp3_manager
{
    /// <summary>
    /// Takes care about localizatin of entire program
    /// </summary>
    public static class LanguageManager
    {
        /// <summary>
        /// Contains data for localozation
        /// </summary>
        static Hashtable stringTable = null;

        public static void Init()
        {
            try
            {
                stringTable = new Hashtable();

                XmlReaderSettings readerSettings = new XmlReaderSettings();
                XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>));
                XmlReader reader = XmlReader.Create(Setting.Enviroment.Lang , readerSettings);
                List<Entry> list = (List<Entry>)serializer.Deserialize(reader);

                foreach (Entry entry in list)
                {
                    stringTable[entry.Key] = entry.Value;
                }

                reader.Close();
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /// <summary>
        /// Gets text by key
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static string GetText(string key)
        {
            if (stringTable.ContainsKey(key))
            {
                return stringTable[key].ToString();
            }

            return key;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="form"></param>
        public static void SetLocalization(Form form)
        {
            try
            {
                SetLocalizationForControls(form.Controls);
            } 
            catch(System.Exception e)
            {
               
            }
        }

        #region private localizatin functions
            private static void SetLocalizationForMenuItems(ToolStripItemCollection menu)
            {
                foreach (ToolStripMenuItem i in menu)
                {
                    if (i.DropDownItems.Count > 0)
                    {
                        SetLocalizationForMenuItems(i.DropDownItems);
                    }
                    if (stringTable.ContainsKey(i.Name))
                    {
                        i.Text = stringTable[i.Name].ToString();
                    }
                }
            }
            private static void SetLocalizationForControls(Control.ControlCollection controls)
            {
                foreach (Control c in controls)
                {
                    if (c.Controls.Count > 0)
                    {
                        SetLocalizationForControls(c.Controls);
                    }
                    if (c is TreeView)
                    {
                        SetLocalizationForTreeNodes(((TreeView)c).Nodes);
                    } else if (c is MenuStrip)
                    {
                        SetLocalizationForMenuItems(((MenuStrip)c).Items);
                    } else if (c is ContextMenuStrip)
                    {
                        SetLocalizationForMenuItems(((ContextMenuStrip)c).Items);
                    }
                    if (stringTable.ContainsKey(c.Name))
                    {
                        c.Text = stringTable[c.Name].ToString();
                    }
                }
            }
            private static void SetLocalizationForTreeNodes(TreeNodeCollection nodes)
        {
            foreach (TreeNode n in nodes)
            {
                if (n.Nodes.Count > 0)
                {
                    SetLocalizationForTreeNodes(n.Nodes);
                }
                if (stringTable.ContainsKey(n.Name))
                {
                    n.Text = stringTable[n.Name].ToString();
                }
            }
        }
        #endregion
    }

    public class Entry
    {
        public object Key;
        public object Value;

        public Entry()
        {
        }

        public Entry(object key, object value)
        {
            Key = key;
            Value = value;
        }
    }
}
